home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / plain / contrib / varindent.tex < prev    next >
Encoding:
Text File  |  1992-08-26  |  3.0 KB  |  61 lines

  1. %From: Donald Arseneau <ASND@TRIUMFCL.BITNET>
  2. %Subject: Variable indentation puzzle
  3. %
  4. %The puzzle in TeXmag V4N1 about making indentation match the length of
  5. %the line above is interesting because there are (at least) two very different
  6. %approaches.  One is to set every paragraph in a \vbox in order to measure the
  7. %width of its last line.  I hope someone sends in a solution using that method
  8. %because I chose the method suggested by the question: "(as per what is done
  9. %with displayed equations)."
  10. %                           The width of the line above a displayed equation
  11. %is given with the parameter \predisplaysize, so it would seem trivial to use
  12. %a displayed equation to measure the line width.   However, there are some
  13. %details to keep things interesting:  The vertical spacing must be the same
  14. %as for an ordinary paragraph, including the \parskip; Likewise for the
  15. %penalties; The depths must be preserved, especially if a pagebreak occurs;
  16. %If the indentation gets too wide something must be done to prevent a bad
  17. %linebreak.  The unboxing method has this last problem too, but there it may
  18. %be possible to reset the final line to the full textwidth.  Here I use
  19. %ordinary indentation when the line gets too long.  The advantage the display
  20. %method has over reboxing is that there are things that cannot be pulled
  21. %of a vertical list, so reboxing won't always work.
  22.  
  23. \newdimen \normalparindent
  24. \newdimen \longlinelen
  25. \newdimen \gscratch % used globally
  26.  
  27. % useful little macro: gives absolute value of a number or a dimension (if in
  28. % a dimension register).  Note that this makes use of TeX's confusing habit of
  29. % expanding \if's within a number
  30.  
  31. \def\abs#1{\ifnum#1<0 -\fi#1}
  32.  
  33. % \usevarindent turns on variable indentation.  Use grouping to turn it off.
  34. % (It would be easy to convert this to a LaTeX environment.)
  35. % It is necessary to say \usevarindent before ending the paragraph
  36. % above the first variable indentation.
  37.  
  38. \def\usevarindent{%
  39. % don't try indenting to within 2\parindent of the right margin
  40.  \normalparindent=\parindent
  41.  \longlinelen=\hsize \advance\longlinelen-2\normalparindent
  42.  \displaywidowpenalty=\widowpenalty
  43.  \let\par=\varindent}
  44.  
  45. % \varindent is used to end each paragraph during variable indentation.
  46.  
  47. \def\varindent{\ifhmode\ifinner\else $$ % test for unrestricted horizontal mode
  48.  \lineskiplimit=-999pt % so we get a baselineskip that we can cancel with:
  49.  \abovedisplayskip=-\baselineskip \abovedisplayshortskip=-\baselineskip
  50.  \belowdisplayskip=0pt \belowdisplayshortskip=0pt
  51.  \halign{##\cr\noalign{\global\gscratch=\prevdepth}% get depth of line above
  52.    \hbox{\vrule width0pt depth \gscratch }\cr}% preserve its depth
  53. % now get the width of the last line of the paragraph that just ended,
  54. % If it is too long just use normal indentation:
  55.  \ifdim\abs\predisplaysize>\longlinelen \global\gscratch=\normalparindent
  56. %  otherwise, use the width of the line above (\predisplaysize-2em):
  57.    \else\global\gscratch=\predisplaysize \global\advance\gscratch -2em\fi
  58. $$\parindent=\gscratch
  59. \fi\fi \endgraf}% Plain TeX sets \endgraf=\par; use \@@par in LaTeX
  60.  
  61.